package com.easyattendance.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyattendance.model.StudentElective;
import com.easyattendance.services.AddStudentService;
import com.easyattendance.services.AddSubjectService;

/**
 * Servlet implementation class StudentElectivesSubmissionServlet
 */
@WebServlet(name = "StudentElectivesSubmissionServlet", urlPatterns = "/StudentElectivesSubmissionServlet")
public class StudentElectivesSubmissionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentElectivesSubmissionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		AddStudentService addStudentService = new AddStudentService();
		StudentElective studElective = new StudentElective();
		AddSubjectService addSubjectService = new AddSubjectService();
		
		String studRollNo[] =request.getParameterValues("stud_roll_no");
		String studId[] =request.getParameterValues("stud_id");
		String subjects[] = new String[studId.length]; //request.getParameterValues("subject");
		String classId = request.getParameter("class_id");
		
		boolean res = false;
		
		for(int i=0; i<studId.length; i++) {
			subjects[i] = request.getParameter("subject"+studRollNo[i]+i);
			
			studElective.setClass_id(classId);
			studElective.setSubject_id(subjects[i]);
			studElective.setStud_roll_no(studRollNo[i]);
			
			if(addStudentService.ifStudentElectiveAlreadyExists(studElective)) {
				System.out.println("Elective for student "+ addStudentService.getStudentByStudentId(studId[i]).getStud_fname()+" already exists..");
			}
			else {
				res = addStudentService.addStudentElective(studElective);
			}
			
			
			
			if(!res) {
				System.out.println("Elective for student "+ addStudentService.getStudentByStudentId(studId[i]).getStud_fname()+" not added..");
			}
			else {
				System.out.println("Elective for student "+ addStudentService.getStudentByStudentId(studId[i]).getStud_fname()+" added successfully....");
			}
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
